/**  
 * @Title: DataXComp.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-04-13 16:21:25 
 */  
package com.fsxgt.datagrid.component.handler.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.fsxgt.datagrid.component.bo.CmpFetchOneBO;
import com.fsxgt.datagrid.component.bo.CmpFetchOneColumnBO;
import com.fsxgt.datagrid.component.service.impl.CmpFetchOneService;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.handler.DbHandler;
import com.fsxgt.datagrid.ds.utils.ParamUtil;
import com.fsxgt.datagrid.ds.utils.ParamUtil.ParamBO;
import com.fsxgt.datagrid.flow.exception.FlowException;
import com.fsxgt.datagrid.flow.handler.FlowContext;
import com.fsxgt.datagrid.flow.handler.FlowReturn;
import com.fsxgt.datagrid.flow.handler.IComponentHandler;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: DataxComponent
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-04-13 16:21:25 
*/
@Slf4j
@Component
public class CmpFetchOneHandler extends IComponentHandler{
	

	@Autowired
	private QueryComponent queryComponent;
	@Autowired
	private CmpFetchOneService cmpFetchOneService;


	public FlowReturn handler(final Object config,FlowContext context) throws Exception{
		
		
		CmpFetchOneBO bo=(CmpFetchOneBO)config;
		
		Map params=(Map) context.get(ParamUtil.PARAM_KEY);
		
		
	 
		

		DbHandler dbHandler = queryComponent.getHandler(Long.valueOf(bo.getDatasourceId()));
		
		
		ParamBO paramBO = ParamUtil.transform(bo.getSqlText(), params);
		
		List<Map<String, Object>> result=dbHandler.queryForList(paramBO.getText(), paramBO.getParams());
		
		log.info(String.format("执行sql:%s,结果:%s",paramBO.getText(),JSON.toJSONString(result)));
		
		
		if(StringUtils.isNotEmpty(bo.getParamAlias())) {
			if(result.size()>0) {
				ParamUtil.putParam(params, bo.getParamAlias(),result.get(0));
				log.info(String.format("变量[%s]赋值:%s",bo.getParamAlias(),result.get(0)));
			}
		}
		
		
		List<CmpFetchOneColumnBO> columnList = bo.getColumnList();
		
		
		List list=new ArrayList();
			
		for (CmpFetchOneColumnBO column : columnList) {
			for (Map m : result) {
				Object value = m.get(column.getSrcColumnName());

				if (StringUtils.isEmpty(column.getValName()) || StringUtils.isEmpty(column.getValColumn())) {
					continue;
				}

				String relation = column.getRelation();
				String srcColumnValue = column.getSrcColumnValue();

				if (ParamUtil.compare(value, srcColumnValue, relation)) {
					if(list.contains(column.getValName())) {
						throw new FlowException("变量["+column.getValName()+"]存在多条记录");
					}else {
						list.add(column.getValName());
					}

					ParamUtil.putParam(params, column.getValName(), m.get(column.getValColumn()));
					log.info(String.format("变量[%s]赋值:%s", column.getValName(), m.get(column.getValColumn())));
				}
			}
		};

		
		return new FlowReturn();
		

		
	}
	
	/**  
	 * @Title: getName
	 * @Description: TODO(描述)
	 * @return 
	 * @see com.fsxgt.datagrid.core.handler.ICommonComponentHandler#getName() 
	 * @author liuyi
	 * @date 2023-04-14 13:49:53 
	 */ 
	@Override
	public String getComponentType() {
		// TODO Auto-generated method stub
		return "Fetch One";
	}




	@Override
	public void stopHandler(Object config, FlowContext context) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
